/**
 * 商家服务类
 * 用于获取商家列表、商家详情等商家相关操作
 */

const request = require('../libs/request');

class OrderService {

    static async getOrderList(page = 1, limit = 10,orderTab = 'all',keyword = '') {
        const response = await request.get('/v1/order/get-order-list', {
            needAuth: true,
            data: {
                page,
                limit,
                order_tab:orderTab,
                keyword:keyword
            }
        });

        return {
            success: response.code === 1,
            data: response.data,
            message: response.msg || '获取订单列表成功'
        };
    }

    /**
     * 检查是否可以进行支付
     */
    static async checkCanPay(orderId) {
        const response = await request.post('/v1/order/check-can-pay', {
            order_id: orderId
        },{
            needAuth: true,
            showErrorInfo: true,
        });

        return {
            success: response.code === 1,
            data: response.data || {},
            message: response.msg || null
        };
    }  

    static async one(orderId){
        const response = await request.get('/v1/order/one', {
            data:{
                order_id: orderId
            },
            needAuth: true,
            showErrorInfo: true,
        });

        return {
            success: response.code === 1,
            data: response.data || {},
            message: response.msg || null
        };
    }

    static async refund(data){
        
        const response = await request.post('/v1/order/refund', data,{
            needAuth: true,
            showErrorInfo: true,
        });

        return {
            success: response.code === 1,
            data: response.data || {},
            message: response.msg || null
        };
    }

    static async doHide(id) {
        try {
            const response = await request.post('/v1/order/do-hide', {
                id: id
            },{
                needAuth: true,
            });

            if (response.code === 1) {
                return {
                    success: true,
                    message: response.msg || '隐藏成功'
                };
            }
            else {
                return {
                    success: false,
                    message: response.msg || '隐藏失败'
                };
            }
        } catch (error) {
            return {
                success: false,
                message: error.message || '网络请求失败'
            };
        }
    }

    static async complaint(orderId, content) {
		try {
			const response = await request.post(`/v1/biz-goods-order/complaint`, {
				order_id: orderId,
				content: content,
				type:'store-order'
			},{
				needAuth: true,
			});

			if (response.code === 1) {
				return {
					success: true,
					message: response.msg || '投诉提交成功'
				};
			} else {
				return {
					success: false,
					message: response.msg || '投诉提交失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				message: error.message || '网络请求失败'
			};
		}
	}

    /**
     * 更新订单快递单号
     */
    static async updateExpressNo(orderId, expressNo) {
        try {
            const response = await request.post('/v1/order/update-express-no', {
                order_id: orderId,
                express_no: expressNo
            }, {
                needAuth: true,
                showErrorInfo: true,
            });

            return {
                success: response.code === 1,
                data: response.data || {},
                message: response.msg || '发货成功'
            };
        } catch (error) {
            return {
                success: false,
                message: error.message || '网络请求失败'
            };
        }
    }

    /**
     * 获取订单详情（兼容现有方法名）
     */
    static async getOrderInfo(orderId) {
        return this.one(orderId);
    }
}

module.exports = OrderService;
